package hot;

/**
 * @author way
 * @create 2025-03-15 16:40
 */
public class _74_searchMatrix {
    public boolean searchMatrix(int[][] matrix, int target) {
        int i = 0;
        int j = matrix[0].length - 1;
        while (i < matrix.length && j >= 0) {
            if (matrix[i][j] == target) return true;

            if (matrix[i][j] > target) j--;
            else i++;
        }
        return false;
    }


    public boolean searchMatrix_binary(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        int l = 0;
        int r = m * n - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (matrix[mid / n][mid % n] > target) {
                r = mid - 1;
            } else if (matrix[mid / n][mid % n] < target) {
                l = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }
}
